// pages/knowledge/knowList.js
const app = getApp()
var backtop = require('../tpl/back2top/back2topTpl.js');

const getInf = (str, key) => str.replace(new RegExp(`${key}`, 'g'), `%%${key}%%`).split('%%');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    page: 1,
    knowsize: 10,   //首页知识的数量
    knowList: [],
    searchInfo: {
      knows: [],
      searchCount: -1
    },
    searchModal: false,
    isBottom: false,
    isLoad: false,
    keywords: '',
    searchLoading: false,
  },

  getPageKnow(page, size) {
    //分页读取知识
    var that = this;
    var urlBase = app.globalData.urlBase;
    wx.request({
      url: urlBase + '/wx/getKnowledgeInfos',
      data: {
        page: page,
        pagesize: size
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.length === 0) {
          that.setData({
            isBottom: true
          });
        } else {
          let list = that.data.knowList.concat(res.data);
          that.setData({
            knowList: list
          });
          // console.log(list)
        }
        that.setData({
          isLoad: false,
        })
        wx.hideLoading()
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading()
    this.getPageKnow(this.data.page, this.data.knowsize);
  },

  onPageScroll: function (e) {
    if (e.scrollTop > 100) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },
 
  goTop: function (e) { 
    backtop.goTop();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log()
    let that = this;
    wx.createSelectorQuery().select('.indexes').boundingClientRect(function (res) {
      that.setData({
        barTop: res.top
      })
    }).exec()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isLoad: true,
    })
    if (this.data.isBottom) {
      setTimeout(() => {
        this.setData({
          isLoad: false,
        })
      }, 2000)
      return false;
    } else {
      this.data.page++;
      let size = this.data.knowsize;
      this.getPageKnow(this.data.page, size);
    }
  },

  /**
   * 搜索知识
   */
  searchKnow() {
    let urlBase = app.globalData.urlBase
    let _self = this;
    let key = this.data.keywords
    if (key == '') 
      return;

    this.setData({
      searchLoading: true
    })
    wx.request({
      url: urlBase + '/wx/search/knowledge',
      method: 'GET',
      data: {
        keywords: key
      },
      success(res) {
        console.log(res.data)
        let list = res.data.knows;
        for (let i = 0; i < list.length; i++ ) {
          list[i].title = getInf(list[i].title, key)
        }
        _self.setData({
          'searchInfo.knows': list,
          'searchInfo.searchCount': res.data.searchCount,
          searchLoading: false
        })
      }
    })
  },

  inputKeyword(e) {
    if(e.detail.value == '') {
      this.setData({
        searchInfo: {
          knows: [],
          searchCount: -1
        },
      })
    }
    // console.log(e.detail.value)
    this.setData({
      keywords: e.detail.value
    })
  },

  showModal() {
    this.setData({
      searchModal: true
    })
  },
  hideModal(e) {
    let bar = this.data.CustomBar;
    var query = wx.createSelectorQuery();
    let _self = this;
    //点击遮罩层关闭
    query.select('.cu-dialog').boundingClientRect(function (rect) {
      if (e.detail.y > (bar + 50 + rect.height)) {
        _self.setData({
          searchModal: false
        })
      }
    }).exec();
  },
  // 解决遮罩层的滚动穿透问题
  catchTouch(){},
  toDetail(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../knowledge/knowDetail/detail?kid=' + id
    })
  },
})